<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>stack封装栈</title>
</head>
<body>
</body>
<script>
    // 基于数组封装栈stack
    function Stack() {
        // 栈内元素
        this.list = [];
        // 1、进栈  选择直接给封装的类Stack的原型注入函数push
        // this.push方式是给类Stack的每一个实例添加函数push，耗费性能
        // this.push = function (params) {
        //     this.list.push(params)
        // }
        Stack.prototype.push = function (params) {
            this.list.push(params);
        }

        // 2、出栈 pop  栈顶元素出栈（删除栈顶元素）,返回删除的元素
        Stack.prototype.pop = function (){
            return this.list.pop();
        }

        // 3、返回栈顶元素 peek ，不会删除栈顶元素，只是查看栈顶元素
        Stack.prototype.peek = function (){
            return this.list[this.list.length-1]
        }

        // 4、查看是否是空 isEmpty
        Stack.prototype.isEmpty = function (){
            return this.list.length === 0;
        }

        // 5、获取个数 size
        Stack.prototype.size = function (){
            return this.list.length;
        }

        // 6、将栈内元素以字符串时展示
        Stack.prototype.toString = function (){
            let stackString = ''
            for(var i = 0; i <= this.list.length-1; i++){
                stackString += this.list[i] + ' '; 
            }
            return stackString;
        }
    }

    // 实例： 

</script>
</html>